//
//  YHJShopCarViewController.m
//  Youhuiju
//
//  Created by yutenys on 16/7/13.
//  Copyright © 2016年 yutenys. All rights reserved.
//

#import "YHJShopCarViewController.h"
#import "ShopTableViewCell.h"
#import "ModelShop.h"
#import "YORequestHander.h"

@interface YHJShopCarViewController ()<UITableViewDelegate, UITableViewDataSource>
{
    BOOL isSelect;
}
@property (nonatomic, strong)UITableView *tableViewShop;
@property (nonatomic, strong)NSMutableArray *arr;//存放本地数组
@property (nonatomic, strong)UIImageView *imageAD;//广告图
@property (nonatomic, strong)UIButton *buttonSure;//确定按钮
@property (nonatomic, strong)UIButton *buttonCancel;//取消按钮
@property (nonatomic, strong)UILabel *labelTitle;//每个产品的标题
@property (nonatomic, strong)UIButton *buttonAllSure;//底部工能栏确定按钮
@property (nonatomic, strong)UIButton *buttonAllCancel;//底部工能栏取消按钮
@property (nonatomic, strong)UILabel *labelAllSelect;//底部工能栏全选
@property (nonatomic, strong)UILabel *labelTotal;//底部工能栏合计
@property (nonatomic, strong)UILabel *labelQuantity;//底部功能栏总计的数量
@property (nonatomic, strong)UILabel *labelMoney;//底部功能栏金额
@property (nonatomic, strong)UIButton *buttonBilling;//底部功能栏结算;
//显示数据源数组
@property (nonatomic, strong)NSMutableArray *dataArr;
// 已选的商品集合
@property (nonatomic, strong)NSMutableArray *selectGoods;

@property (nonatomic, weak)NSIndexPath *indexPathRow;

@property (nonatomic, strong)UIView *viewBackgrond;

@property (nonatomic, strong)ShopTableViewCell *cell;
//头部
@property (nonatomic, strong)UILabel *labelFootTitle;
@property (nonatomic, strong)UIImageView *imageFoot;
//购买数量
@property (nonatomic, assign)NSInteger count;

@property (nonatomic, strong)UIImageView *imageCoupon;//图片
@property (nonatomic, strong)UILabel *labelAir;//购物车空空
@property (nonatomic, strong)UILabel *Overall;// 全场一折起
@property (nonatomic, strong)UIButton *buttonGoShopping;

@property (nonatomic, strong)YORequestHander *requestHandler;
@end

@implementation YHJShopCarViewController


- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:YES];
    
    [_selectGoods removeAllObjects];
    isSelect = NO;
   
    
    _labelMoney.text = [NSString stringWithFormat:@"￥0.00"];
}



- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"购物车";
    _buttonCancel.selected = NO;
    self.view.backgroundColor = [UIColor colorWithHexString:@"#f5f5f5"];
    
    self.dataArr = [NSMutableArray array];
    self.selectGoods = [NSMutableArray array];
    
    
    NSMutableArray *arr = @[@"1",@"2"].mutableCopy;
    if ([arr isEqualToArray:@[]]) {
        
        
        [self createPlaceholder];
        
        
        
    }else {
    
    [self createImageAD];
  
    [self createData];
    [self createTableView];
    }
  
    
    
    // Do any additional setup after loading the view.
}

#pragma mark -- 购物车为空
- (void)createPlaceholder {
    
    self.imageCoupon = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"Shopping-icon"]];
    [self.view addSubview:self.imageCoupon];
    [self.imageCoupon mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.centerX.equalTo(self.view);
        make.top.equalTo(self.view.mas_top).offset(132*FitH);
        make.width.mas_equalTo(135*FitW);
        make.height.mas_equalTo(135*FitH);
    }];
    
    self.labelAir = [[UILabel alloc]init];
    self.labelAir.textColor = [UIColor colorWithHexString:@"cccccc"];
    self.labelAir.text = @"购物车空空的呦 !";
    self.labelAir.font = [UIFont systemFontOfSize:18*FitW];
    [self.view addSubview:self.labelAir];
    [self.labelAir mas_makeConstraints:^(MASConstraintMaker *make) {
       
        
        make.top.equalTo(self.imageCoupon.mas_bottom).offset(38*FitH);
        make.centerX.equalTo(self.view);
        
        
        
    }];
    self.Overall = [[UILabel alloc]init];
    self.Overall.textColor = [UIColor colorWithHexString:@"cccccc"];
    self.Overall.text = @"全场一折起";
    self.Overall.font = [UIFont systemFontOfSize:14*FitW];
    [self.view addSubview:self.Overall];
    [self.Overall mas_makeConstraints:^(MASConstraintMaker *make) {
        
        
        make.top.equalTo(self.labelAir.mas_bottom).offset(13*FitH);
        make.centerX.equalTo(self.view);
        
        
        
    }];
    
    self.buttonSure = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.buttonSure setImage:[UIImage imageNamed:@"To-collect"] forState:UIControlStateNormal];
    
    
    
    [self.buttonSure addTarget:self action:@selector(goshoppingAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.buttonSure];
    [self.buttonSure mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.Overall.mas_bottom).offset(74*FitH);
        make.left.equalTo(self.view.mas_left).offset(17*FitW);
        make.width.mas_equalTo(341*FitW);
        make.height.mas_equalTo(45*FitH);
        
        
    }];
    
    
    
}
//去抢购
- (void)goshoppingAction:(UIButton *)button {
    
    self.tabBarController.selectedViewController = [self.tabBarController.viewControllers objectAtIndex:0];
   
}





#pragma mark -- 购物车有商品
//获取数据
- (void)getData {
    
    [self.requestHandler getWithAPIName:nil apiVersion:nil parameters:nil cachIdentifier:nil success:^(id response) {
        
        
        
        
        
    } failure:^(NSString *errorMessage) {
       
        
        
    }];
    
    
    
    
    
    
}

//计算已选商品金额
- (void)countPrice {
    double totlePrice = 0.0;
    
    
    for (ModelShop *model in _selectGoods) {
        
        
        double price = [model.price doubleValue];
        totlePrice += price*model.number;
        
        
        //_labelQuantity.text = [NSString stringWithFormat:@"共%ld件",model.number];
        
    }
    int a = totlePrice;
    self.labelQuantity.text = [NSString stringWithFormat:@"共%d件",a/100];
    
   
    
    
    
    _labelMoney.text = [NSString stringWithFormat:@"￥%.2f",totlePrice];
    double baoyouprice = 90 - totlePrice;
    
    if (baoyouprice > 0) {
        
        self.labelFootTitle.text = [NSString stringWithFormat:@"还差%@元享受包邮",[NSString stringWithFormat:@"%.2f",baoyouprice]];
    }else {
        self.labelFootTitle.text = @"已包邮";
    }
    

    
    
   
}

- (void)createData {
    for (int i = 0; i < 4; i++) {
        ModelShop *model = [[ModelShop alloc]init];
        
        model.nameStr = @"商品名称";
        model.price = @"100";
        model.number = 1;
        model.image = [UIImage imageNamed:@"niuroU.jpg"];
        [_dataArr addObject:model];
    }
    
   
        [self.tableViewShop reloadData];
    

}


//创建广告图
- (void)createImageAD {
    
    if (!_imageAD) {
        self.imageAD = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"ljzgouwuche"] ];
        [self.view addSubview:_imageAD];
        
        [_imageAD mas_makeConstraints:^(MASConstraintMaker *make) {
           
            make.top.equalTo(self.view.mas_top).offset(64);
            make.height.mas_equalTo(47*FitH);
            make.left.equalTo(self.view.mas_left);
            make.right.equalTo(self.view.mas_right);
            
        }];
        
        
    }
    
}
//底部结算功能栏
- (void)createBilling {
    
    self.viewBackgrond = [[UIView alloc]init];
    [self.view addSubview:self.viewBackgrond];
    self.viewBackgrond.backgroundColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0];
    [self.viewBackgrond mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
        make.bottom.equalTo(self.view.mas_bottom).offset(-49);
        make.height.mas_equalTo(47*FitH);
        
    }];
    
    
    
    
    if (!_buttonAllSure) {
        
        
        self.buttonAllSure = [ UIButton buttonWithType:UIButtonTypeCustom];
        [self.buttonAllSure setImage:[UIImage imageNamed:@"ljzgouwuchedianji"] forState:UIControlStateNormal];
        [self.buttonAllSure addTarget:self action:@selector(allSureAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.viewBackgrond addSubview:_buttonAllSure];
        
        [_buttonAllSure mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.equalTo(self.viewBackgrond.mas_top).offset(13*FitH);
            make.bottom.equalTo(self.viewBackgrond.mas_bottom).offset(-15*FitH);
            make.left.equalTo(self.viewBackgrond.mas_left).offset(9*FitW);
            make.width.mas_equalTo(19*FitW);
            
        }];
        
    }
    
    if (!_buttonAllCancel) {
        
        
        self.buttonAllCancel = [ UIButton buttonWithType:UIButtonTypeCustom];
        [self.buttonAllCancel setImage:[UIImage imageNamed:@"ljzquxiao"] forState:UIControlStateNormal];
        [self.buttonAllCancel addTarget:self action:@selector(allCancelAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.viewBackgrond addSubview:_buttonAllCancel];
        
        [_buttonAllCancel mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.equalTo(self.viewBackgrond.mas_top).offset(13*FitH);
            make.bottom.equalTo(self.viewBackgrond.mas_bottom).offset(-15*FitH);
            make.left.equalTo(self.viewBackgrond.mas_left).offset(9*FitW);
            make.width.mas_equalTo(19*FitW);
            
        }];
        
    }
    
    
    _buttonAllSure.alpha = 0;
    _buttonAllCancel.alpha = 1;
    
    if (!_buttonBilling) {
        
        
        self.buttonBilling = [ UIButton buttonWithType:UIButtonTypeSystem];
        self.buttonBilling.tintColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0];
        [self.buttonBilling setTitle:@"结算" forState:UIControlStateNormal];
        [self.buttonBilling addTarget:self action:@selector(billingAction:) forControlEvents:UIControlEventTouchUpInside];
        
        self.buttonBilling.backgroundColor = [UIColor colorWithHexString:@"ff7836"];
        
        [self.buttonBilling.layer setCornerRadius:15.0];
        
        [self.viewBackgrond addSubview:_buttonBilling];
        
        [_buttonBilling mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.viewBackgrond.mas_top).offset(8*FitH);
            make.bottom.equalTo(self.viewBackgrond.mas_bottom).offset(-8*FitH);
            make.right.equalTo(self.viewBackgrond.mas_right).offset(-11*FitW);
            make.width.mas_equalTo(126*FitW);
        }];
        
    }
    
    if (!_labelAllSelect) {
        
        
        self.labelAllSelect = [[UILabel alloc]init];
        _labelAllSelect.text = @"全选";
        
        _labelAllSelect.font = [UIFont systemFontOfSize:13*FitH];
        
        [self.viewBackgrond addSubview:_labelAllSelect];
        [_labelAllSelect mas_makeConstraints:^(MASConstraintMaker *make) {
            
            
            make.top.equalTo(self.viewBackgrond.mas_top).offset(17*FitH);
            make.bottom.equalTo(self.viewBackgrond.mas_bottom).offset(-18*FitH);
            make.left.equalTo(_buttonAllCancel.mas_right).offset(15*FitW);
            make.height.mas_equalTo(10*FitH);
            
            
        }];
        
        
    }
    
    
    if (!_labelTotal) {
        
        
        self.labelTotal= [[UILabel alloc]init];
        _labelTotal.text = @"合计";
        _labelTotal.textColor = [UIColor colorWithHexString:@"737373"];
        _labelTotal.font = [UIFont systemFontOfSize:10*FitH];
        
        [self.viewBackgrond addSubview:_labelTotal];
        [_labelTotal mas_makeConstraints:^(MASConstraintMaker *make) {
            
            
            make.top.equalTo(self.viewBackgrond.mas_top).offset(8*FitH);
            make.bottom.equalTo(self.viewBackgrond.mas_bottom).offset(-28*FitH);
            make.left.equalTo(self.viewBackgrond.mas_left).offset(82*FitW);
            make.height.mas_equalTo(10*FitH);
            
            
        }];
        
        
    }
    
    if (!_labelMoney) {
        
        
        self.labelMoney= [[UILabel alloc]init];
        _labelMoney.text = [NSString stringWithFormat:@"￥0.00"];
        
        _labelMoney.font = [UIFont systemFontOfSize:10*FitH];
        _labelMoney.textColor = [UIColor colorWithHexString:@"ff7836"];
        
        [self.viewBackgrond addSubview:_labelMoney];
        [_labelMoney mas_makeConstraints:^(MASConstraintMaker *make) {
            
            
            make.top.equalTo(self.viewBackgrond.mas_top).offset(8*FitH);
            make.bottom.equalTo(self.viewBackgrond.mas_bottom).offset(-28*FitH);
            make.left.equalTo(_labelTotal.mas_right).offset(6*FitW);
            make.width.mas_equalTo(50*FitW);
            
            
        }];
        
        
    }
    
    if (!_labelQuantity) {
        
        
        self.labelQuantity= [[UILabel alloc]init];
        _labelQuantity.text = @"共0件";
        _labelQuantity.textColor = [ UIColor colorWithHexString:@"737373"];
        
        _labelQuantity.font = [UIFont systemFontOfSize:10*FitH];
        
        [self.viewBackgrond addSubview:_labelQuantity];
        
        
        [_labelQuantity mas_makeConstraints:^(MASConstraintMaker *make) {
            
            
            make.top.equalTo(_labelTotal.mas_top).offset(10*FitH);
            make.bottom.equalTo(self.viewBackgrond.mas_bottom).offset(-8*FitH);
            make.left.equalTo(self.viewBackgrond.mas_left).offset(82*FitW);
            make.height.mas_equalTo(2*FitH);
            
            
        }];
        
        
    }
    
    
}


//创建 tableView
- (void)createTableView {
   // if (!_tableViewShop) {
        self.tableViewShop = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableViewShop.delegate = self;
        _tableViewShop.dataSource = self;
        [self.view addSubview:_tableViewShop];
        [_tableViewShop registerNib:[UINib nibWithNibName:@"ShopTableViewCell" bundle:nil] forCellReuseIdentifier:@"ShopTableViewCell"];
        
        [_tableViewShop mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_imageAD.mas_bottom).offset(7*FitH);
            make.bottom.equalTo(self.view.mas_bottom).offset(-100*FitH);
            make.right.equalTo(self.view.mas_right);
            make.left.equalTo(self.view.mas_left);
            
        }];
        
        [self createBilling];
        
   // }
}

//tableView 协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataArr.count;
    
}
#pragma mark -- 添加头部
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
   
    
    UIView *viewHead = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 39*FitH)];
    viewHead.backgroundColor = [UIColor whiteColor];
    self.buttonSure = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.buttonSure setImage:[UIImage imageNamed:@"ljzgouwuchedianji"] forState:UIControlStateNormal];
    [self.buttonSure addTarget:self action:@selector(sureAction:) forControlEvents:UIControlEventTouchUpInside];
    [viewHead addSubview:self.buttonSure];
    
    self.buttonCancel = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.buttonCancel setImage:[UIImage imageNamed:@"ljzquxiao"] forState:UIControlStateNormal];
    [self.buttonCancel addTarget:self action:@selector(cancelAction:) forControlEvents:UIControlEventTouchUpInside];
    [viewHead addSubview:self.buttonCancel];
    /** 初始状态保持没有点击的状态, 隐藏确定适合的状态 */
    self.buttonCancel.alpha = 1;
    self.buttonSure.alpha = 0;
    
    [self.buttonSure mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(viewHead.mas_top).offset(10*FitH);
        make.bottom.equalTo(viewHead.mas_bottom).offset(-10*FitH);
        make.left.equalTo(viewHead.mas_left).offset(8*FitW);
        make.width.mas_equalTo(19*FitW);
        
    }];
    
    
    [self.buttonCancel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(viewHead.mas_top).offset(10*FitH);
        make.bottom.equalTo(viewHead.mas_bottom).offset(-10*FitH);
        make.left.equalTo(viewHead.mas_left).offset(8*FitW);
        make.width.mas_equalTo(19*FitW);
        
    }];
    
    
    
    //添加标头 并约束
    self.labelTitle = [[UILabel alloc]init];
    _labelTitle.font = [UIFont systemFontOfSize:13];
    _labelTitle.text = @"澳洲进口优质牛肉";
    _labelTitle.textColor = [UIColor colorWithHexString:@"#494949"];
    [viewHead addSubview:_labelTitle];
    [self.labelTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(viewHead.mas_top).offset(13*FitH);
      
        
        make.left.equalTo(_buttonSure.mas_right).offset(15*FitW);
        make.bottom.equalTo(viewHead.mas_bottom).offset(-13*FitH);
      
    }];
    
    
    return viewHead;
    
}
#pragma mark --添加尾部
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
    
  
    UIView *viewFoot = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 44*FitH)];
    viewFoot.backgroundColor = [UIColor whiteColor];
  
        self.labelFootTitle = [[UILabel alloc]init];
        
        double baoyouprice = 90;

        
        
        self.labelFootTitle.text = [NSString stringWithFormat:@"还差%@元享受包邮",[NSString stringWithFormat:@"%.2f",baoyouprice]];
        
        [viewFoot addSubview:self.labelFootTitle];
        
        _labelFootTitle.textColor = [UIColor colorWithHexString:@"494949"];
        _labelFootTitle.font = [UIFont systemFontOfSize:13*FitW];
        [_labelFootTitle mas_makeConstraints:^(MASConstraintMaker *make) {
           
            
            make.top.equalTo(viewFoot.mas_top).offset(17*FitH);
            make.left.equalTo(viewFoot.mas_left).offset(40*FitW);
            make.bottom.equalTo(viewFoot.mas_bottom).offset(-17*FitH);
            
            
            
        }];
        
    return viewFoot;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return 44*FitH;
}


#pragma mark ---- 每个栏目顶部取消按钮点击事件
- (void)sureAction:(UIButton *)button {
   
    if (!isSelect) {
        for (ModelShop *model in _dataArr) {
            [_selectGoods addObject:model];
        }
    }else {
        [_selectGoods removeAllObjects];
        
    }
    [_selectGoods removeAllObjects];
    [self countPrice];
   // [self.tableViewShop reloadData];
    
    [[NSNotificationCenter defaultCenter]postNotificationName:@"buttonAllCancel" object:nil];

    
    
    self.buttonSure.alpha = 0;
    self.buttonCancel.alpha = 1;
    self.buttonAllCancel.alpha = 1;
    self.buttonAllSure.alpha = 0;
   
   
    
    
}
- (void)cancelAction:(UIButton *)button {
    
    //点击全选时,
    self.buttonCancel.alpha = 0;
    self.buttonSure.alpha = 1;
    self.buttonAllCancel.alpha = 0;
    self.buttonAllSure.alpha = 1;
    
    
    [_selectGoods removeAllObjects];
    
    
    [[NSNotificationCenter defaultCenter]postNotificationName:@"buttonAllSure" object:nil];
    
    if (!isSelect) {
        for (ModelShop *model in _dataArr) {
            [_selectGoods addObject:model];
        }
    }else {
        [_selectGoods removeAllObjects];
        
    }

    [self countPrice];
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 39*FitH;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ShopTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ShopTableViewCell"];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    
    //选择回调
    //添加数据
    cell.cancelBlock = ^(BOOL isSelec){
        if (isSelec) {
            [_selectGoods addObject:[_dataArr objectAtIndex:indexPath.row]];
            
        }
        
    
     
        //如果数据和选择的商品一样为全选
        if (_selectGoods.count == _dataArr.count) {
            _buttonAllSure.selected = YES;
            _buttonAllCancel.selected = NO;
            if (_buttonAllSure.selected == YES & _buttonAllCancel.selected == NO ) {
                
                _buttonAllCancel.alpha = 0;
                _buttonAllSure.alpha = 1;
                _buttonCancel.alpha = 0 ;
                _buttonSure.alpha = 1;
                
            }
          
 
          
         
        }
        
        
         [self countPrice];
       
        
    };
    
    //清除数据
    cell.sureBlock = ^(BOOL isSele){
        
        
       
        
        if (isSele) {
            [_selectGoods removeObject:[_dataArr objectAtIndex:indexPath.row]];
        }
        
        //如果数据为零为全部取消
        if ( _selectGoods.count != _dataArr.count) {
            
            _buttonAllSure.selected = NO;
            _buttonAllCancel.selected = YES;
            if (_buttonAllSure.selected == NO & _buttonAllCancel.selected == YES ) {
                
                _buttonAllCancel.alpha = 1;
                _buttonAllSure.alpha = 0;
                _buttonCancel.alpha = 1 ;
                _buttonSure.alpha = 0;
                //[self.tableViewShop reloadData];
            }
    }
        
        
        [self countPrice];
       
        
    };
    
    
    __block ShopTableViewCell *weakCell = cell;
    
    cell.numAddBlock = ^() {
      
        NSInteger count = [weakCell.labelDigtal.text integerValue];
        count++;
        NSString *numStr = [NSString stringWithFormat:@"%ld",(long)count];
        
        ModelShop *model = [_dataArr objectAtIndex:indexPath.row];
        
        weakCell.labelDigtal.text = numStr;
        model.number = count;
        
        [_dataArr replaceObjectAtIndex:indexPath.row withObject:model];
        
        if ([_selectGoods containsObject:model]) {
            [_selectGoods removeObject:model];
            [_selectGoods addObject:model];
            
        }
        [self countPrice];
    };
    
    
    
    cell.numCutBlock = ^() {
      
        NSInteger count = [weakCell.labelDigtal.text integerValue];
        count--;
        if(count <= 0){
            return ;
        }
        NSString *numStr = [NSString stringWithFormat:@"%ld",(long)count];
        
        ModelShop *model = [_dataArr objectAtIndex:indexPath.row];
        
        weakCell.labelDigtal.text = numStr;
        
        model.number = count;
        [_dataArr replaceObjectAtIndex:indexPath.row withObject:model];
        
        //判断已选择数组里有无该对象,有就删除  重新添加
        if ([_selectGoods containsObject:model]) {
            [_selectGoods removeObject:model];
            [_selectGoods addObject:model];
            [self countPrice];
        }

        [self countPrice];
        
        
        
    };
    
    //更新每行信息
    [cell addModel:[_dataArr objectAtIndex:indexPath.row]];
    
    
    return cell;
    
    
    
}

- (void)replaceTableViewShopAtIndexes {
    [self.tableViewShop reloadData];
}






- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 116*FitH;
}

#pragma mark -- 结算功能点击事件
//全部选住
- (void)allCancelAction:(UIButton *)button {
    
    //点击全选时,
    [_selectGoods removeAllObjects];

    
    [[NSNotificationCenter defaultCenter]postNotificationName:@"buttonAllSure" object:nil];
    
    if (!isSelect) {
        for (ModelShop *model in _dataArr) {
            [_selectGoods addObject:model];
        }
    }else {
        [_selectGoods removeAllObjects];
        
    }
    //[self.tableViewShop reloadData];
    [self countPrice];
    self.buttonAllSure.alpha = 1;
    self.buttonAllCancel.alpha = 0;
    self.buttonSure.alpha = 1;
    self.buttonCancel.alpha = 0;
    
}
//全选取消
- (void)allSureAction:(UIButton *)button {
    
 
    if (!isSelect) {
        for (ModelShop *model in _dataArr) {
            [_selectGoods addObject:model];
        }
    }else {
        [_selectGoods removeAllObjects];
        
    }
    [_selectGoods removeAllObjects];
    [self countPrice];
    //[self.tableViewShop reloadData];
    
    
    
    self.buttonAllCancel.alpha = 1;
    self.buttonAllSure.alpha = 0;
    self.buttonSure.alpha = 0;
    self.buttonCancel.alpha = 1;


    [[NSNotificationCenter defaultCenter]postNotificationName:@"buttonAllCancel" object:nil];
   






}


- (void)billingAction:(UIButton *)button {
    NSLog(@"跳转结算页面");
    
}

#pragma mark - 侧滑删除以选商品
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
    return @"删除";
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    
    
    [_selectGoods removeObject:[_dataArr objectAtIndex:indexPath.row]];
    [self.dataArr removeObjectAtIndex:indexPath.row];
    [self.tableViewShop deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    
    
    [self countPrice];
    [self.tableViewShop reloadData];
    
   
//        [_selectGoods removeObject:[_dataArr objectAtIndex:indexPath.row]];
//
//
//    //如果数据为零为全部取消
//    if ( _selectGoods.count != _dataArr.count) {
//
//        _buttonAllSure.selected = NO;
//        _buttonAllCancel.selected = YES;
//        if (_buttonAllSure.selected == NO & _buttonAllCancel.selected == YES ) {
//
//            _buttonAllCancel.alpha = 1;
//            _buttonAllSure.alpha = 0;
//            _buttonCancel.alpha = 1 ;
//            _buttonSure.alpha = 0;
//            //[self.tableViewShop reloadData];
//        }
//    }
    
    
    // [self.tableViewShop reloadData];
    
   // [[NSNotificationCenter defaultCenter]postNotificationName:@"buttonAllCancel" object:nil];
    
    
    
    
    
    
    
    


    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pr2agma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
